import axios from "@/api/interceptor";
import {
    AgentCreateReq,
    AgentDetail,
    AgentPagination,
    AgentRes,
    AgentResetKnowledgeBasesReq,
    AgentResetPluginsReq,
    AgentUpdateReq,
    GetAgentListParam
} from "@/types/agentType";
import {getToken} from "@/utils/auth";
import Taro from "@tarojs/taro";


// 获取所有智能体
export function queryAgentAll(): Promise<AgentPagination> {
  return axios.get('api/v1/sapper/agent/all', {});
}

// 获取所有智能体
export function queryPublicAgentList(params?: GetAgentListParam): Promise<AgentPagination> {
  return axios.get('api/v1/sapper/agent/all/public', {
    params
  });
}

// 获取智能体详情
export function queryAgentDetail(uuid: string): Promise<AgentDetail> {
    return axios.get(`api/v1/sapper/agent/${uuid}`);
}

// 获取智能体列表（分页）
export function queryAgentList(params?: GetAgentListParam): Promise<AgentPagination> {
  try {
    return axios.get('api/v1/sapper/agent',
      { params },
    );
  } catch (error) {
    console.error("API 调用失败", error);
    throw error;
  }
}


// 创建智能体
export function createAgentAPI(data: AgentCreateReq): Promise<AgentRes> {
    return axios.post(`api/v1/sapper/agent`, data);
}

// 更新智能体
export function updateAgentAPI(uuid: string, data: AgentUpdateReq) {
    return axios.put(`api/v1/sapper/agent/${uuid}`, data);
}

// 添加插件到智能体
export function resetAgentPluginAPI(uuid: string, data: AgentResetPluginsReq) {
    return axios.post(`api/v1/sapper/agent/${uuid}/plugin`, data);
}

// 添加知识库到智能体
export function resetAgentKnowledgeBaseAPI(uuid: string, data: AgentResetKnowledgeBasesReq) {
    return axios.post(`api/v1/sapper/agent/${uuid}/knowledge_base`, data);
}

// 删除智能体
export function deleteAgentAPI(pk: string) {
    return axios.delete(`api/v1/sapper/agent/${pk}`);
}


export function generateSplFormStream(uuid: string) {
    try {
        // 使用 fetch 请求来处理流式响应
        return fetch(`${process.env.TARO_APP_BASE_URL}api/v1/sapper/agent/generate_spl_form/${uuid}`, {
            headers: {
                Authorization: `Bearer ${getToken()}`,  // 认证 token
            }
        });
    } catch (error) {
        console.error('Error while streaming data:', error);
    }
}


export function generateAnswerStream(uuid: string) {
  return Taro.connectSocket({
    url: `${process.env.TARO_APP_BASE_WS_URL}api/v1/sapper/agent/ws/generate_answer/${uuid}`,
    header: {
      'content-type': 'application/json',
      // Authorization: `Bearer ${getToken()}`,
    }
  })
}

export function generateSplChainStream(uuid: string) {
    try {
        // 使用 fetch 请求来处理流式响应
        return fetch(`${process.env.TARO_APP_BASE_URL}api/v1/sapper/agent/generate_spl_chain/${uuid}`, {
            headers: {
                Authorization: `Bearer ${getToken()}`,  // 认证 token
            }
        });
    } catch (error) {
        console.error('Error while streaming data:', error);
    }
}

// 下载智能体：返回 Response 对象
export async function downloadAgentAPI(uuid: string): Promise<Response> {
    return fetch(`${process.env.TARO_APP_BASE_URL}api/v1/sapper/agent/download/${uuid}`, {
        method: 'POST',
        headers: {
            Authorization: `Bearer ${getToken()}`,
        }
    });
}

